package tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kc
 * @date 2021/4/20 0020 0:04
 */
public class TankFrame extends Frame {

    static int WIDTH =  PropertyMgr.getInt("gameWidth");
    static int HEIGHT = PropertyMgr.getInt("gameHeight");

    Tank myTank = new Tank(200, 400, Dir.UP, Group.GOOD, false, this);
    List<Bullet> bullets = new ArrayList<>();
    List<Tank> tanks = new ArrayList<>();
    List<Explode> explodes = new ArrayList<>();

    public TankFrame() {

        this.setVisible(true);
        this.setSize(WIDTH, HEIGHT);
        this.setTitle("tank war");
        this.setResizable(false);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.addKeyListener(new MyKeyListener());
    }

    Image image = null;

    @Override
    public void update(Graphics g) {
        if (null == image) {
            image = this.createImage(WIDTH, HEIGHT);
        }
        Graphics ig = image.getGraphics();
        Color color = ig.getColor();
        ig.setColor(Color.BLACK);
        ig.fillRect(0, 0, WIDTH, HEIGHT);
        ig.setColor(color);
        paint(ig);
        g.drawImage(image, 0, 0, null);
    }

    @Override
    public void paint(Graphics graphics) {
        Color c = graphics.getColor();
        graphics.setColor(Color.WHITE);
        graphics.drawString("bullets" + bullets.size(), 10, 60);
        graphics.drawString("tanks" + tanks.size(), 10, 80);
        graphics.setColor(c);

        myTank.paint(graphics);
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(graphics);
        }
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(graphics);
        }

        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(graphics);
        }
    }

    class MyKeyListener extends KeyAdapter {

        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            setMainTankDir();
//            new Thread(this::run).start();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setMainTankDir();
        }

        private void setMainTankDir() {

            if (bL) {
                myTank.setDir(Dir.LEFT);
            }
            if (bD) {
                myTank.setDir(Dir.DOWN);
            }
            if (bR) {
                myTank.setDir(Dir.RIGHT);
            }
            if (bU) {
                myTank.setDir(Dir.UP);
            }

            myTank.setMoving(bL || bD || bR || bU);
        }

        private void run() {
            new Audio("audio/tank_move.wav").play();
        }
    }
}
